<?php
namespace app\admin\controller\system;
use app\common\annotation\NodeAnnotation;
use app\common\traits\ControllerCurd;
use think\Console;
use think\facade\Cookie;
use think\Request;

/**
 * @NodeAnnotation(
 *     title="系统管理",
 *     is_view=0,
 *     icon="/material/setting.png"
 * )
 */
class Setting extends \app\common\controller\Admin
{

    use ControllerCurd;

    public $model;

    public function initialize()
    {
        parent::initialize();
        $this->model = new \app\admin\model\system\Option();
    }

    /**
     * @NodeAnnotation(
     *     title="站点设置",
     *      parent="admin/system.setting",
     *     is_view=1,
     *     is_menu=1,
     *     MenuRoot="admin/system.setting"
     * )
     */
    public function site(Request $request)
    {
        $site = $this->model->where("option_name",'site_info')->find();

        if($request->isPost() && $request->isAjax())
        {
            $input = $request->post();

            $site_info = [
                "option_name"  => "site_info",
                "option_value" => $input['site_info']
            ];

            if ($site)
            {
                $res = $this->model->where("id",$site['id'])->update($site_info);
            }else{
                $res = $this->model->create($site_info);
            }

            if($res)
            {
                $this->success("保存成功");
            }else{
                $this->error("保存失败");
            }
        }

        $this->assign([
            "site" => $site['option_value']
        ]);

        return $this->fetch();
    }


    /**
     * @NodeAnnotation(
     *     title="系统设置",
     *      parent="admin/system.setting",
     *     is_view=1,
     *     is_menu=1,
     *     MenuRoot="admin/system.setting"
     * )
     */
    public function systemsetting(Request $request)
    {
        $site = $this->model->where("option_name",'system_setting')->find();

        if($request->isPost() && $request->isAjax())
        {
            $input = $request->post();

            $setting_info = [
                "option_name"  => "system_setting",
                "option_value" => $input['system']
            ];

            if ($site)
            {
                $res = $this->model->where("id",$site['id'])->update($setting_info);
            }else{
                $res = $this->model->create($setting_info);
            }

            if($res)
            {
                $this->success("保存成功");
            }else{
                $this->error("保存失败");
            }
        }

        $this->assign([
            "setting" => $site['option_value']
        ]);

        return $this->fetch();
    }

    /**
     * @NodeAnnotation(
     *     title="缓存请里",
     *      parent="admin/system.setting",
     *     is_view=1,
     *     is_menu=0,
     *     MenuRoot="admin/system.setting",
     *     auth=0
     * )
     */
    public function clearcache()
    {
        clear_cache();
//        Console::call('clear' );
        return $this->fetch();
    }

    /**
     * @NodeAnnotation(
     *     title="退出系统",
     *      parent="admin/system.setting",
     *     is_view=1,
     *     is_menu=0,
     *     MenuRoot="admin/system.setting",
     *     auth=0
     * )
     */
    public function logout()
    {
        Cookie::delete("adminUser");
        $this->success("退出成功",url('admin/login/index'));
    }

    /**
     * @NodeAnnotation(
     *     title="生成数据字缓存",
     *      parent="admin/system.setting",
     *     is_view=0,
     *     is_menu=0,
     *     MenuRoot="admin/system.setting",
     *     auth=0
     * )
     */
    public function createtablefield()
    {
        $this->success(command_exec('php think optimize:schema'));
    }

    /**
     * @NodeAnnotation(
     *     title="生成类库映射",
     *      parent="admin/system.setting",
     *     is_view=0,
     *     is_menu=0,
     *     MenuRoot="admin/system.setting",
     *     auth=0
     * )
     */
    public function createclass()
    {
        $this->success(command_exec('php think optimize:autoload'));
    }

    /**
     * @NodeAnnotation(
     *     title="生成配置缓存",
     *      parent="admin/system.setting",
     *     is_view=0,
     *     is_menu=0,
     *     MenuRoot="admin/system.setting",
     *     auth=0
     * )
     */
    public function createconfig()
    {
        $this->success(command_exec('php think optimize:config'));
    }


    /**
     * @NodeAnnotation(
     *     title="上传设置",
     *      parent="admin/system.setting",
     *     is_view=1,
     *     is_menu=1,
     *     MenuRoot="admin/system.setting"
     * )
     */
    public function upload(Request $request)
    {
        $uploadSetting = get_option("upload_setting");

        if ($request->isPost())
        {
            $uploadSetting = $this->request->post();
            set_option('upload_setting', $uploadSetting);
            $this->success('保存成功！');
        }

        $this->assign([
            "upload_setting" => $uploadSetting
        ]);

        return $this->fetch();
    }


    /**
     * @NodeAnnotation(
     *     title="下载设置",
     *      parent="admin/system.setting",
     *     is_view=1,
     *     is_menu=1,
     *     MenuRoot="admin/system.setting"
     * )
     */
    public function download_setting(Request $request)
    {
        $downloadSetting = get_option("download_setting");

        if ($request->isPost())
        {
            $uploadSetting = $this->request->post();
            set_option('download_setting', $uploadSetting);
            $this->success('设置成功！');
        }
        $this->assign([
            "download_setting" => $downloadSetting
        ]);
        return $this->fetch();
    }



}